﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.IO;

namespace HCLSMod
{
    /// <summary>
    /// 文件传输系统常量
    /// </summary>
    public class FileTRConst
    {
        public const int PacketSize = 200000;        
    }
    /// <summary>
    /// CRC32管理
    /// </summary>
    public class CRC32Util
    {
        private static int[] CRC16_TABLE = {  
        0x0000, 0x1189, 0x2312, 0x329b, 0x4624, 0x57ad, 0x6536, 0x74bf,  
        0x8c48, 0x9dc1, 0xaf5a, 0xbed3, 0xca6c, 0xdbe5, 0xe97e, 0xf8f7,  
        0x1081, 0x0108, 0x3393, 0x221a, 0x56a5, 0x472c, 0x75b7, 0x643e,  
        0x9cc9, 0x8d40, 0xbfdb, 0xae52, 0xdaed, 0xcb64, 0xf9ff, 0xe876,  
        0x2102, 0x308b, 0x0210, 0x1399, 0x6726, 0x76af, 0x4434, 0x55bd,  
        0xad4a, 0xbcc3, 0x8e58, 0x9fd1, 0xeb6e, 0xfae7, 0xc87c, 0xd9f5,  
        0x3183, 0x200a, 0x1291, 0x0318, 0x77a7, 0x662e, 0x54b5, 0x453c,  
        0xbdcb, 0xac42, 0x9ed9, 0x8f50, 0xfbef, 0xea66, 0xd8fd, 0xc974,  
        0x4204, 0x538d, 0x6116, 0x709f, 0x0420, 0x15a9, 0x2732, 0x36bb,  
        0xce4c, 0xdfc5, 0xed5e, 0xfcd7, 0x8868, 0x99e1, 0xab7a, 0xbaf3,  
        0x5285, 0x430c, 0x7197, 0x601e, 0x14a1, 0x0528, 0x37b3, 0x263a,  
        0xdecd, 0xcf44, 0xfddf, 0xec56, 0x98e9, 0x8960, 0xbbfb, 0xaa72,  
        0x6306, 0x728f, 0x4014, 0x519d, 0x2522, 0x34ab, 0x0630, 0x17b9,  
        0xef4e, 0xfec7, 0xcc5c, 0xddd5, 0xa96a, 0xb8e3, 0x8a78, 0x9bf1,  
        0x7387, 0x620e, 0x5095, 0x411c, 0x35a3, 0x242a, 0x16b1, 0x0738,  
        0xffcf, 0xee46, 0xdcdd, 0xcd54, 0xb9eb, 0xa862, 0x9af9, 0x8b70,  
        0x8408, 0x9581, 0xa71a, 0xb693, 0xc22c, 0xd3a5, 0xe13e, 0xf0b7,  
        0x0840, 0x19c9, 0x2b52, 0x3adb, 0x4e64, 0x5fed, 0x6d76, 0x7cff,  
        0x9489, 0x8500, 0xb79b, 0xa612, 0xd2ad, 0xc324, 0xf1bf, 0xe036,  
        0x18c1, 0x0948, 0x3bd3, 0x2a5a, 0x5ee5, 0x4f6c, 0x7df7, 0x6c7e,  
        0xa50a, 0xb483, 0x8618, 0x9791, 0xe32e, 0xf2a7, 0xc03c, 0xd1b5,  
        0x2942, 0x38cb, 0x0a50, 0x1bd9, 0x6f66, 0x7eef, 0x4c74, 0x5dfd,  
        0xb58b, 0xa402, 0x9699, 0x8710, 0xf3af, 0xe226, 0xd0bd, 0xc134,  
        0x39c3, 0x284a, 0x1ad1, 0x0b58, 0x7fe7, 0x6e6e, 0x5cf5, 0x4d7c,  
        0xc60c, 0xd785, 0xe51e, 0xf497, 0x8028, 0x91a1, 0xa33a, 0xb2b3,  
        0x4a44, 0x5bcd, 0x6956, 0x78df, 0x0c60, 0x1de9, 0x2f72, 0x3efb,  
        0xd68d, 0xc704, 0xf59f, 0xe416, 0x90a9, 0x8120, 0xb3bb, 0xa232,  
        0x5ac5, 0x4b4c, 0x79d7, 0x685e, 0x1ce1, 0x0d68, 0x3ff3, 0x2e7a,  
        0xe70e, 0xf687, 0xc41c, 0xd595, 0xa12a, 0xb0a3, 0x8238, 0x93b1,  
        0x6b46, 0x7acf, 0x4854, 0x59dd, 0x2d62, 0x3ceb, 0x0e70, 0x1ff9,  
        0xf78f, 0xe606, 0xd49d, 0xc514, 0xb1ab, 0xa022, 0x92b9, 0x8330,  
        0x7bc7, 0x6a4e, 0x58d5, 0x495c, 0x3de3, 0x2c6a, 0x1ef1, 0x0f78  
                                           };

        /// <summary>
        /// 计算一个字节数组的CRC值 
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static int GetCRC32(byte[] data)
        {
            int fvalue = 0xffff;
            for (int i = 0; i < data.Length; i++)
            {
                // 1.value 右移8位(相当于除以256)  
                // 2.value与进来的数据进行异或运算后再与0xFF进行与运算  
                //    得到一个索引index，然后查找CRC16_TABLE表相应索引的数据  
                // 1和2得到的数据再进行异或运算。  
                fvalue = (fvalue >> 8) ^ CRC16_TABLE[(fvalue ^ data[i]) & 0xff];
            }
            return ~fvalue & 0xffff;
        }
    }
    /// <summary>
    /// 文件传输客户端基础类
    /// </summary>
    public class FileTRClientBase
    {
        protected int fPacketSize = FileTRConst.PacketSize;
        /// <summary>
        /// 数据包大小
        /// 设置后会初始化数据
        /// </summary>
        public int PacketSize { get { return fPacketSize; } }
        /// <summary>
        /// 控件是否下载文件
        /// </summary>
        public bool Running
        {
            get { return fDeamonRun; }
            set
            {
                fDeamon.Enabled = value;
                fDeamonRun = value;
            }
        }
        // 定时器
        protected System.Timers.Timer fDeamon = new System.Timers.Timer();
        protected bool fDeamonRun = false;
        public int LastError { get; set; }

        public int Interval
        {
            get { return (int)fDeamon.Interval; }
            set { fDeamon.Interval = value; }
        }

        public FileTRClientBase()
        {
            fDeamon.AutoReset = false;
            fDeamon.Interval = 100;
            fDeamon.Elapsed += fDeamon_Elapsed;
            LastError = 0;
        }

        void fDeamon_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {

        }
    }
    /// <summary>
    /// 文件传送器
    /// </summary>
    public class FileTransfer
    {
        string fFileName = "";
        /// <summary>
        /// 文件名
        /// </summary>
        public string FileName { get { return fFileName; } }

        Guid fGuid = Guid.NewGuid();
        /// <summary>
        /// 令牌字符串
        /// </summary>
        public string Token { get { return fGuid.ToString().ToUpper(); } }
        /// <summary>
        /// 令牌GUID
        /// </summary>
        public Guid GUID { get { return fGuid; } }
       
        DateTime fCreateTime = DateTime.Now;
        /// <summary>
        /// 文件对象已经休眠时间长度
        /// </summary>
        public double SecondsPass { get { return (DateTime.Now - fCreateTime).TotalSeconds; } }
        /// <summary>
        /// 文件数据
        /// </summary>
        byte[] fFileBuf = new byte[0];
        /// <summary>
        /// 文件数据包数量
        /// </summary>
        public int PacketCount
        {
            get
            {                
                int fPCount = fFileBuf.Length / fPacketSize;
                if ((fFileBuf.Length % fPacketSize) > 0)
                    fPCount++;
                return fPCount;
            }
        }
        public int FileSize { get { return fFileBuf.Length; } }

        int fPacketSize = FileTRConst.PacketSize;

        /// <summary>
        /// 附属信息
        /// </summary>
        int fDocID = 0;
        public int DocID { get { return fDocID; }  }
        public object Tag {get;set;}
        public FileTransfer(string _fFileName)
        {
            if (File.Exists(_fFileName))
            {
                fFileName = _fFileName;
                FileStream fFS = new FileStream(fFileName, FileMode.Open, FileAccess.Read);
                try
                {
                    fFileBuf = new byte[fFS.Length];
                    fFS.Seek(0, SeekOrigin.Begin);
                    fFS.Read(fFileBuf, 0, fFileBuf.Length);
                }
                finally
                {
                    fFS.Close();
                }
            }
        }
        public FileTransfer(int _fDocID,string _fFileName)
            : this(_fFileName)
        {
            fDocID = _fDocID;
        }

        /// <summary>
        /// 获得文件中指定序号的数据包
        /// </summary>
        /// <param name="fPIndex">数据包序号，从0开始</param>
        /// <param name="iCRC32">成功返回CRC32，如果文件已经设置，大小不为0，返回0，否则返回-1</param>
        /// <returns></returns>
        public byte[] GetFilePacket(int fPIndex,out int iCRC32)
        {
            byte[] fBuf = new byte[0];
            iCRC32 = 0;
            if ((fFileName != "") && (fPIndex < PacketCount) && (fPIndex >= 0))
            {
                int fiStar = fPIndex * fPacketSize;
                if (fPIndex < (PacketCount - 1))
                    fBuf = new byte[fPacketSize];
                else
                    fBuf = new byte[fFileBuf.Length - (fPacketSize * (PacketCount - 1))];
                Buffer.BlockCopy(fFileBuf, fiStar, fBuf, 0, fBuf.Length);
                iCRC32 = CRC32Util.GetCRC32(fBuf);
                fCreateTime = DateTime.Now;
            }
            else
                if (PacketCount==0)
                    iCRC32 = -1;
            return fBuf;
        }
        /// <summary>
        /// 获得指定序号的数据包
        /// </summary>
        /// <param name="fPacketIndex">数据包序号，如果超出范围，该值返回-1，函数返回0</param>
        /// <param name="fPacketCount">返回数据包数量</param>
        /// <param name="fDataPacket">返回的数据包数据</param>
        /// <returns>获得正确数据(包括文件存在，但序号超界，超界的CRC32为0)返回数据包的CRC32，否则返回-1（标示文件可能不存在）</returns>
        public int GetFilePacket(ref int fPacketIndex, out int fPacketCount, out byte[] fDataPacket)
        {
            fPacketCount = this.PacketCount;
            int fiCRC32 = 0;
            fDataPacket = GetFilePacket(fPacketIndex, out fiCRC32);
            if (fiCRC32<0)
                fPacketIndex = -1;
            return fiCRC32;
        }
    }

    #region 下载控件
    /// <summary>
    /// 接口：文档下载接口
    /// </summary>
    public interface IFileDownLoader
    {
        int DownLoadFileDataPacket(int fFileID, ref int fPacketIndex, out int fPacketCount, out byte[] fDataPacket);
    }
    /// <summary>
    /// 文档下载器事件调用
    /// </summary>
    /// <param name="fFileID"></param>
    /// <param name="fPacketIndex"></param>
    /// <param name="fPacketCount"></param>
    /// <param name="fFileBuf"></param>
    public delegate void FileDownloaderCallBack(int fFileID, int fPacketIndex, int fPacketCount, byte[] fFileBuf);
    /// <summary>
    /// 文档下载器
    /// </summary>
    public class FileDownloader : FileTRClientBase
    {        
        /// <summary>
        /// 文件下载器
        /// </summary>
        IFileDownLoader fDownLoader = null;
        /// <summary>
        /// 下载操作执行后返回调用事件
        /// </summary>
        public event FileDownloaderCallBack OnDownLoadNotify;
        // 当前操作的数据包索引
        int fPacketIndex = -1;
        int fPacketCount = 0;
        int fFileID = 0;
        /// <summary>
        /// 文件ID，设置后会初始化
        /// </summary>
        public int FileID
        {
            get { return fFileID; }
            set
            {
                fFileID = value;
                fPacketCount = 0;
                fPacketIndex = -1;
                fBufList.Clear();
            }
        }
        // 收到的数据包列表
        Dictionary<int, byte[]> fBufList = new Dictionary<int, byte[]>();

        public FileDownloader(IFileDownLoader _fDownLoader):base()
        {
            fDownLoader = _fDownLoader;
            fDeamon.Elapsed += fDeamon_Elapsed;
            fErrorCount = 0;
        }

        public FileDownloader(int _fFileID, IFileDownLoader _fDownLoader)
            : this(_fDownLoader)
        {
            FileID = _fFileID;
        }
        int fErrorCount = 0;
        void fDeamon_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            fDeamon.Enabled = false;
            try
            {
                if (fDownLoader != null)
                {
                    byte[] fBuf = new byte[0];
                    if (fPacketCount <= 0)
                    {
                        fErrorCount = 0;
                        // 获得数据包数量
                        fPacketIndex = -1;
                        if (fDownLoader.DownLoadFileDataPacket(fFileID, ref fPacketIndex, out fPacketCount, out fBuf) >= 0)
                        {
                            fPacketIndex = 0;
                            if (OnDownLoadNotify != null)
                                OnDownLoadNotify(fFileID, fPacketIndex, fPacketCount, fBuf);
                        }
                    }
                    else
                    {
                        int fiCRC32 = fDownLoader.DownLoadFileDataPacket(fFileID, ref fPacketIndex, out fPacketCount, out fBuf);
                        // 下载数据包
                        if ((fiCRC32 > 0) && (fiCRC32==CRC32Util.GetCRC32(fBuf)))
                        {
                            // 如果fPacketIndex>=0表示获得正确的数据包、
                            if (fPacketIndex < 0)
                                fBufList.Clear();
                            else
                            {
                                if (fBufList.ContainsKey(fPacketIndex))
                                    fBufList.Remove(fPacketIndex);
                                fBufList.Add(fPacketIndex, fBuf);
                            }
                            fPacketIndex++;
                            // 数据包已经接收完成
                            if (fPacketIndex >= fPacketCount)
                            {
                                Running = false;
                                // 返回收到的所有数据
                                if (OnDownLoadNotify != null)
                                    OnDownLoadNotify(fFileID, fPacketIndex, fPacketCount, fGetAllPacketData());
                            }
                            else
                                if (OnDownLoadNotify != null)
                                    OnDownLoadNotify(fFileID, fPacketIndex, fPacketCount, fBuf);
                        }
                        else
                        {
                            fErrorCount++;
                            if (fErrorCount > 3)
                            {
                                Running = false;
                                // 发生错误！
                                if (OnDownLoadNotify != null)
                                    OnDownLoadNotify(fFileID, -1, -1, fBuf);
                            }
                        }
                    }
                }
            }
            finally
            {
                fDeamon.Enabled = fDeamonRun;
            }
        }
        /// <summary>
        /// 返回收到的所有数据
        /// </summary>
        /// <returns></returns>
        protected byte[] fGetAllPacketData()
        {
            MemoryStream fBufStream = new MemoryStream();
            for (int i = 0; i < fBufList.Count; i++)
                fBufStream.Write(fBufList[i], 0, fBufList[i].Length);
            return fBufStream.ToArray();
        }
    }
    #endregion 

    /// <summary>
    /// 文件接收器
    /// </summary>
    public class FileReceiver
    {
        #region 成员
        // 数据包大小
        int fPacketSize = FileTRConst.PacketSize;
        // 文件大小
        int fFileSize = 0;
        int fFileRecCount = 0;

        Guid fGuid = Guid.NewGuid();
        /// <summary>
        /// 令牌字符串
        /// </summary>
        public string Token { get { return fGuid.ToString().ToUpper(); } }
        /// <summary>
        /// 令牌GUID
        /// </summary>
        public Guid GUID { get { return fGuid; } }
        DateTime fCreateTime = DateTime.Now;
        /// <summary>
        /// 文件对象已经休眠时间长度
        /// </summary>
        public double SecondsPass { get { return (DateTime.Now - fCreateTime).TotalSeconds; } }
        /// <summary>
        /// 文件数据包数量
        /// </summary>
        public int PacketCount
        {
            get
            {
                int fPCount = fFileSize / fPacketSize;
                if ((fFileSize % fPacketSize) > 0)
                    fPCount++;
                return fPCount;
            }
        }

        Dictionary<int, byte[]> fFilePacketList = new Dictionary<int, byte[]>();
        public bool HasGetAllPacket
        {
            get
            {
                if (PacketCount > 0)
                {
                    for (int i = 0; i < PacketCount; i++)
                        if (!fFilePacketList.ContainsKey(i))
                            return false;
                    return true;
                }
                else
                    return false;
            }
        }

        /// <summary>
        /// 附属信息
        /// </summary>
        int fDocID = 0;
        public int DocID { get { return fDocID; } }
        public object Tag { get; set; }
        #endregion

        /// <summary>
        /// 构建函数
        /// </summary>
        /// <param name="_fFileSize">要接收的文件大小</param>
        public FileReceiver(int _fFileSize)
        {
            fFileSize = _fFileSize;
        }
        /// <summary>
        /// 接收数据包
        /// </summary>
        /// <param name="fPIndex">数据包索引号</param>
        /// <param name="fData">数据包</param>
        /// <param name="CRC32">数据包校验码</param>
        /// <returns></returns>
        public int PutFilePacket(int fPIndex, byte[] fData, int iCRC32)
        {
            int iR = 0;
            if ((fPIndex >= 0) && (fPIndex < this.PacketCount) && (!this.fFilePacketList.ContainsKey(fPIndex)) && (iCRC32 == CRC32Util.GetCRC32(fData)))
            {
                this.fFilePacketList.Add(fPIndex, fData);
                iR = fData.Length;
            }
            return iR;
        }

        /// <summary>
        /// 检查是否收到指定序号的数据包
        /// </summary>
        /// <param name="fIndex">数据包序号，从0开始</param>
        /// <returns></returns>
        public bool HasRecPacket(int fIndex)
        {
            return fFilePacketList.ContainsKey(fIndex);
        }
        /// <summary>
        /// 将收到的数据保存为文件
        /// </summary>
        /// <param name="fFileName">目标文件</param>
        /// <param name="fOverWirte">是否覆盖，默认覆盖</param>
        /// <returns></returns>
        public bool SaveFileTo(string fFileName, bool fOverWirte = true)
        {
            bool iR = false;
            if (this.HasGetAllPacket)
            {
                if (File.Exists(fFileName) && fOverWirte)
                    File.Delete(fFileName);
                if (!File.Exists(fFileName))
                {
                    using (FileStream fSTemp = new FileStream(fFileName, FileMode.Create, FileAccess.Write))
                    {
                        for (int i = 0; i < this.PacketCount; i++)
                        {
                            byte[] fbuf = this.fFilePacketList[i];
                            fSTemp.Write(fbuf, 0, fbuf.Length);
                        }
                    }
                    iR = File.Exists(fFileName);
                }
            }
            return iR;
        }
    }

    #region 上传控件
    public interface IFileUpLoader
    {
        int UpLoadFileDataPacket(ref string fUploadToken, int fPacketIndex,ref int fPacketCount, byte[] fDataPacket);
    }

    public delegate void FileUploaderCallBack(object sender, int fPacketIndex, int fPacketCount);
    public class FileUploader : FileTRClientBase
    {
        #region 成员

        FileInfo fDestFile = null;
        public string FileName
        {
            get
            {
                return (fDestFile != null) ? fDestFile.FullName : "";
            }
            set
            {
                fDestFile = null;
                if (File.Exists(value))
                    fDestFile = new FileInfo(value);
            }
        }
        public int PacketCount
        {
            get
            {
                int fPCount =0;
                if (fDestFile != null)
                {
                    fPCount = (int)fDestFile.Length / fPacketSize;
                    if ((fDestFile.Length % fPacketSize) > 0)
                        fPCount++;
                }
                return fPCount;
            }
        }
        public long FileSize { get { return (fDestFile != null) ? fDestFile.Length : 0; } }
        public int PacketIdx { get; set; }
        string fUploadToken = "";
        public string UploadToken { get { return fUploadToken; } }
        /// <summary>
        /// 文件下载器
        /// </summary>
        IFileUpLoader fUpLoader = null;
        /// <summary>
        /// 下载操作执行后返回调用事件
        /// </summary>
        public event FileUploaderCallBack OnUpLoadNotify;

        #endregion
        public FileUploader(IFileUpLoader _fUpLoader):base()
        {
            fUpLoader = _fUpLoader;
            fDeamon.Elapsed += fDeamon_Elapsed;
            PacketIdx = 0;
        }
        public FileUploader(string _fFileName, IFileUpLoader _fUpLoader)
            : this(_fUpLoader)
        {
            FileName = _fFileName;
        }
        void fDeamon_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            fDeamon.Enabled = false;
            try
            {
                int fPCount = PacketCount;
                if ((fDestFile != null) && (fPCount > 0))
                {
                    if ((PacketIdx >= 0) && (PacketIdx < fPCount))
                    {
                        byte[] fBuf = new byte[0];
                        using (FileStream fDestFBuf = fDestFile.OpenRead())
                        {
                            fDestFBuf.Position = PacketIdx * fPacketSize;                            
                            if ((PacketIdx + 1) * fPacketSize < fDestFile.Length)
                                fBuf = new byte[fPacketSize];
                            else
                                fBuf = new byte[fDestFile.Length - PacketIdx * fPacketSize];
                            fDestFBuf.Read(fBuf, 0, fBuf.Length);                            
                        }

                        LastError = fUpLoader.UpLoadFileDataPacket(ref this.fUploadToken, PacketIdx,ref  fPCount, fBuf);
                        if ((LastError > 0) && (fPCount==PacketCount))
                        {
                            PacketIdx++;
                            if (this.OnUpLoadNotify != null)
                                OnUpLoadNotify(this, PacketIdx, fPCount);
                            LastError = 0;
                        }
                        else
                        {
                            this.fUploadToken = "";
                            PacketIdx = 0;
                            //fDeamonRun = false;
                            //if (this.OnUpLoadNotify != null)
                            //OnUpLoadNotify(this, LastError, 0);  
                        }
                    }
                    if ((PacketIdx >= 0) && (PacketIdx == PacketCount))
                        fDeamonRun = false;
                }
            }
            finally
            {
                fDeamon.Enabled = fDeamonRun;
            }
        }
    }
    #endregion

    /// <summary>
    /// 文件传输服务器
    /// </summary>
    public class FileTranService
    {
        Dictionary<string, FileTransfer> fFTList = new Dictionary<string, FileTransfer>();
        Dictionary<string, FileReceiver> fFRList = new Dictionary<string, FileReceiver>();
        System.Timers.Timer fDeamon = new System.Timers.Timer();
        int fSecondTimeOut = 600;
        /// <summary>
        /// 超时时长，单位：秒
        /// </summary>
        public int SecondTimeOut
        {
            get
            {
                return fSecondTimeOut;
            }
            set
            {
                if (value < 60)
                    fSecondTimeOut = 60;
                else
                    if (value > 3600)
                        fSecondTimeOut = 3600;
                    else
                        fSecondTimeOut = value;
            }
        }

        public FileTranService()
        {
            fDeamon.AutoReset = true;
            fDeamon.Enabled = false;
            fDeamon.Interval = 1000;
            fDeamon.Elapsed += fDeamon_Elapsed;
            fDeamon.Enabled = true;
        }
        /// <summary>
        /// 增加文件传输器
        /// </summary>
        /// <param name="fKey"></param>
        /// <param name="fFileTransfer"></param>
        /// <returns></returns>
        public void AddFileTraner(object fKey, FileTransfer fFileTransfer)
        {
            if (fFTList.ContainsKey(fKey.ToString()))
                fFTList.Remove(fKey.ToString());
            fFTList.Add(fKey.ToString(), fFileTransfer);
        }
        public void AddFileReceer(object fKey, FileReceiver fFileTransfer)
        {
            if (fFRList.ContainsKey(fKey.ToString()))
                fFRList.Remove(fKey.ToString());
            fFRList.Add(fKey.ToString(), fFileTransfer);
        }
        /// <summary>
        /// 定时将长时间不活动的传输器关闭
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void fDeamon_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            fDeamon.Enabled = false;
            try
            {
                if (fFTList.Count > 0)
                {
                    // 将操作等待时间过长的传输器关闭
                    List<string> fWaritDel = new List<string>();
                    // 文件发送器
                    foreach (var fFTraner in fFTList)
                    {
                        if (fFTraner.Value.SecondsPass > SecondTimeOut)
                            fWaritDel.Add(fFTraner.Key);
                    }
                    if (fWaritDel.Count > 0)
                        foreach (string fiKey in fWaritDel)
                            fFTList.Remove(fiKey);
                    fWaritDel.Clear();
                    // 文件接收器
                    foreach (var fFTraner in fFRList)
                    {
                        if (fFTraner.Value.SecondsPass > SecondTimeOut)
                            fWaritDel.Add(fFTraner.Key);
                    }
                    if (fWaritDel.Count > 0)
                        foreach (string fiKey in fWaritDel)
                            fFRList.Remove(fiKey);
                    fWaritDel.Clear();
                }
            }
            catch { }
            finally
            {
                fDeamon.Enabled = true;
            }
        }

        /// <summary>
        /// 以传输器的Key获得传输器
        /// </summary>
        /// <param name="fKey"></param>
        /// <returns></returns>
        public FileTransfer GetFileTranerByKey(object fKey)
        {
            if (fFTList.ContainsKey(fKey.ToString()))
                return fFTList[fKey.ToString()];
            else
                return null;
        }
        public FileReceiver GetFileRecerByKey(object fKey)
        {
            if (fFRList.ContainsKey(fKey.ToString()))
                return fFRList[fKey.ToString()];
            else
                return null;
        }
        /// <summary>
        /// 以传输器的文件名获得传输器
        /// </summary>
        /// <param name="fFileName"></param>
        /// <returns></returns>
        public FileTransfer GetFileTranerByFileName(string fFileName)
        {
            foreach (var fFTraner in fFTList)
                if (fFTraner.Value.FileName.ToUpper() == fFileName.ToUpper())
                    return fFTraner.Value;
            return null;
        }
        /// <summary>
        /// 以传输器的文档ID获得传输器
        /// </summary>
        /// <param name="fDocID"></param>
        /// <returns></returns>
        public FileTransfer GetFileTranerByFileID(int fDocID)
        {
            foreach (var fFTraner in fFTList)
                if (fFTraner.Value.DocID == fDocID)
                    return fFTraner.Value;
            return null;
        }
        /// <summary>
        /// 以传输器的GUID获得传输器
        /// </summary>
        /// <param name="fGUID"></param>
        /// <returns></returns>
        public FileTransfer GetFileTranerByGUID(Guid fGUID)
        {
            foreach (var fFTraner in fFTList)
                if (fFTraner.Value.GUID == fGUID)
                    return fFTraner.Value;
            return null;
        }
        public FileReceiver GetFileReceerByGUID(Guid fGUID)
        {
            foreach (var fFReceer in fFRList)
                if (fFReceer.Value.GUID == fGUID)
                    return fFReceer.Value;
            return null;
        }
    }     
}

